;;;; -*- mode: Scheme; indent-tabs-mode: nil; fill-column: 80; -*-
;;;; 
;;;; Copyright © 2015 Rémi Delrue <asgeir@free.fr>
;;;; 
;;;; This program is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;; 
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;; 
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;; see fs.scm comment -- Maxime (2020)
(define-module (rehash binding-utils)
  #:use-module (ice-9 match)
  #:use-module ((srfi srfi-1) #:select (find))
  #:use-module (rnrs bytevectors)
  #:use-module (system foreign)
  #:export (or% and=>%))

;; a variant of OR for foreign pointers.
;; ex:                       (or% 'a 'b) → A
;;                (or% %null-pointer 'b) → B
;;     (or% %null-pointer %null-pointer) → #f
(define-syntax or%
  (syntax-rules ()
    ((_ x)       (let ((x* x))
                   (if (eq? %null-pointer x*)
                       #f
                       x*)))
    ((_ x y ...) (let ((x* x))
                   (if (eq? %null-pointer x*)
                       (or% y ...)
                       x*)))))

;; a variant of and=> for foreign pointers.
(define-syntax-rule (and=>% x f)
  (let ((x* x))
    (if (eq? %null-pointer x*)
        #f
        (f x*))))
